package com.hgb.mall.mbg.mapper;

import com.hgb.mall.mbg.entity.PmsProductCategory;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.dynamic.sql.BasicColumn;
import org.mybatis.dynamic.sql.delete.DeleteDSLCompleter;
import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider;
import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider;
import org.mybatis.dynamic.sql.select.CountDSLCompleter;
import org.mybatis.dynamic.sql.select.SelectDSLCompleter;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
import org.mybatis.dynamic.sql.update.UpdateDSL;
import org.mybatis.dynamic.sql.update.UpdateDSLCompleter;
import org.mybatis.dynamic.sql.update.UpdateModel;
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
import org.mybatis.dynamic.sql.util.SqlProviderAdapter;
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils;

import javax.annotation.Generated;
import java.util.List;
import java.util.Optional;

import static com.hgb.mall.mbg.mapper.PmsProductCategoryDynamicSqlSupport.*;
import static org.mybatis.dynamic.sql.SqlBuilder.isEqualTo;

@Mapper
public interface PmsProductCategoryMapper {
    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    BasicColumn[] selectList = BasicColumn
            .columnList(id, parentId, name, level, productCount, productUnit, navStatus, showStatus, sort, icon, keywords, description);

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    static UpdateDSL<UpdateModel> updateAllColumns(PmsProductCategory record, UpdateDSL<UpdateModel> dsl) {
        return dsl.set(parentId).equalTo(record::getParentId)
                  .set(name).equalTo(record::getName)
                  .set(level).equalTo(record::getLevel)
                  .set(productCount).equalTo(record::getProductCount)
                  .set(productUnit).equalTo(record::getProductUnit)
                  .set(navStatus).equalTo(record::getNavStatus)
                  .set(showStatus).equalTo(record::getShowStatus)
                  .set(sort).equalTo(record::getSort)
                  .set(icon).equalTo(record::getIcon)
                  .set(keywords).equalTo(record::getKeywords)
                  .set(description).equalTo(record::getDescription);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    static UpdateDSL<UpdateModel> updateSelectiveColumns(PmsProductCategory record, UpdateDSL<UpdateModel> dsl) {
        return dsl.set(parentId).equalToWhenPresent(record::getParentId)
                  .set(name).equalToWhenPresent(record::getName)
                  .set(level).equalToWhenPresent(record::getLevel)
                  .set(productCount).equalToWhenPresent(record::getProductCount)
                  .set(productUnit).equalToWhenPresent(record::getProductUnit)
                  .set(navStatus).equalToWhenPresent(record::getNavStatus)
                  .set(showStatus).equalToWhenPresent(record::getShowStatus)
                  .set(sort).equalToWhenPresent(record::getSort)
                  .set(icon).equalToWhenPresent(record::getIcon)
                  .set(keywords).equalToWhenPresent(record::getKeywords)
                  .set(description).equalToWhenPresent(record::getDescription);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @SelectProvider(type = SqlProviderAdapter.class, method = "select")
    long count(SelectStatementProvider selectStatement);

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @DeleteProvider(type = SqlProviderAdapter.class, method = "delete")
    int delete(DeleteStatementProvider deleteStatement);

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @InsertProvider(type = SqlProviderAdapter.class, method = "insert")
    @SelectKey(statement = "SELECT LAST_INSERT_ID()", keyProperty = "record.id", before = false, resultType = Long.class)
    int insert(InsertStatementProvider<PmsProductCategory> insertStatement);

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @SelectProvider(type = SqlProviderAdapter.class, method = "select")
    @ResultMap("PmsProductCategoryResult")
    Optional<PmsProductCategory> selectOne(SelectStatementProvider selectStatement);

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @SelectProvider(type = SqlProviderAdapter.class, method = "select")
    @Results(id = "PmsProductCategoryResult", value = {
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "parent_id", property = "parentId", jdbcType = JdbcType.BIGINT),
            @Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
            @Result(column = "level", property = "level", jdbcType = JdbcType.INTEGER),
            @Result(column = "product_count", property = "productCount", jdbcType = JdbcType.INTEGER),
            @Result(column = "product_unit", property = "productUnit", jdbcType = JdbcType.VARCHAR),
            @Result(column = "nav_status", property = "navStatus", jdbcType = JdbcType.INTEGER),
            @Result(column = "show_status", property = "showStatus", jdbcType = JdbcType.INTEGER),
            @Result(column = "sort", property = "sort", jdbcType = JdbcType.INTEGER),
            @Result(column = "icon", property = "icon", jdbcType = JdbcType.VARCHAR),
            @Result(column = "keywords", property = "keywords", jdbcType = JdbcType.VARCHAR),
            @Result(column = "description", property = "description", jdbcType = JdbcType.LONGVARCHAR)
    })
    List<PmsProductCategory> selectMany(SelectStatementProvider selectStatement);

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @UpdateProvider(type = SqlProviderAdapter.class, method = "update")
    int update(UpdateStatementProvider updateStatement);

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default long count(CountDSLCompleter completer) {
        return MyBatis3Utils.countFrom(this::count, pmsProductCategory, completer);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int delete(DeleteDSLCompleter completer) {
        return MyBatis3Utils.deleteFrom(this::delete, pmsProductCategory, completer);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int deleteByPrimaryKey(Long id_) {
        return delete(c ->
                c.where(id, isEqualTo(id_))
        );
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int insert(PmsProductCategory record) {
        return MyBatis3Utils.insert(this::insert, record, pmsProductCategory, c ->
                c.map(parentId).toProperty("parentId")
                 .map(name).toProperty("name")
                 .map(level).toProperty("level")
                 .map(productCount).toProperty("productCount")
                 .map(productUnit).toProperty("productUnit")
                 .map(navStatus).toProperty("navStatus")
                 .map(showStatus).toProperty("showStatus")
                 .map(sort).toProperty("sort")
                 .map(icon).toProperty("icon")
                 .map(keywords).toProperty("keywords")
                 .map(description).toProperty("description")
        );
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int insertSelective(PmsProductCategory record) {
        return MyBatis3Utils.insert(this::insert, record, pmsProductCategory, c ->
                c.map(parentId).toPropertyWhenPresent("parentId", record::getParentId)
                 .map(name).toPropertyWhenPresent("name", record::getName)
                 .map(level).toPropertyWhenPresent("level", record::getLevel)
                 .map(productCount).toPropertyWhenPresent("productCount", record::getProductCount)
                 .map(productUnit).toPropertyWhenPresent("productUnit", record::getProductUnit)
                 .map(navStatus).toPropertyWhenPresent("navStatus", record::getNavStatus)
                 .map(showStatus).toPropertyWhenPresent("showStatus", record::getShowStatus)
                 .map(sort).toPropertyWhenPresent("sort", record::getSort)
                 .map(icon).toPropertyWhenPresent("icon", record::getIcon)
                 .map(keywords).toPropertyWhenPresent("keywords", record::getKeywords)
                 .map(description).toPropertyWhenPresent("description", record::getDescription)
        );
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default Optional<PmsProductCategory> selectOne(SelectDSLCompleter completer) {
        return MyBatis3Utils.selectOne(this::selectOne, selectList, pmsProductCategory, completer);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default List<PmsProductCategory> select(SelectDSLCompleter completer) {
        return MyBatis3Utils.selectList(this::selectMany, selectList, pmsProductCategory, completer);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default List<PmsProductCategory> selectDistinct(SelectDSLCompleter completer) {
        return MyBatis3Utils.selectDistinct(this::selectMany, selectList, pmsProductCategory, completer);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default Optional<PmsProductCategory> selectByPrimaryKey(Long id_) {
        return selectOne(c ->
                c.where(id, isEqualTo(id_))
        );
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int update(UpdateDSLCompleter completer) {
        return MyBatis3Utils.update(this::update, pmsProductCategory, completer);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int updateByPrimaryKey(PmsProductCategory record) {
        return update(c ->
                c.set(parentId).equalTo(record::getParentId)
                 .set(name).equalTo(record::getName)
                 .set(level).equalTo(record::getLevel)
                 .set(productCount).equalTo(record::getProductCount)
                 .set(productUnit).equalTo(record::getProductUnit)
                 .set(navStatus).equalTo(record::getNavStatus)
                 .set(showStatus).equalTo(record::getShowStatus)
                 .set(sort).equalTo(record::getSort)
                 .set(icon).equalTo(record::getIcon)
                 .set(keywords).equalTo(record::getKeywords)
                 .set(description).equalTo(record::getDescription)
                 .where(id, isEqualTo(record::getId))
        );
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int updateByPrimaryKeySelective(PmsProductCategory record) {
        return update(c ->
                c.set(parentId).equalToWhenPresent(record::getParentId)
                 .set(name).equalToWhenPresent(record::getName)
                 .set(level).equalToWhenPresent(record::getLevel)
                 .set(productCount).equalToWhenPresent(record::getProductCount)
                 .set(productUnit).equalToWhenPresent(record::getProductUnit)
                 .set(navStatus).equalToWhenPresent(record::getNavStatus)
                 .set(showStatus).equalToWhenPresent(record::getShowStatus)
                 .set(sort).equalToWhenPresent(record::getSort)
                 .set(icon).equalToWhenPresent(record::getIcon)
                 .set(keywords).equalToWhenPresent(record::getKeywords)
                 .set(description).equalToWhenPresent(record::getDescription)
                 .where(id, isEqualTo(record::getId))
        );
    }
}
